package com.nopay.cdxphone.ui.ativity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseActivity;
import com.nopay.cdxphone.wx.WXSDKManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.ActivityInviteFriendsBinding;

public class InviteFriendsActivity extends BaseActivity<ActivityInviteFriendsBinding> {

    private static final String APP_TEMP_DIR = Environment.getExternalStorageDirectory().getPath() + "/ChangDianSales/";

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(InviteFriendsActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        binding.tvShare.setOnClickListener(view -> {
//            ClipboardUtils.copyText(Constants.APK_PAGE_URL);
//            ToastUtils.showShort("下载地址已粘贴到剪贴板");
//            try {
//                Intent intent = new Intent(Intent.ACTION_MAIN);
//                ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
//                intent.addCategory(Intent.CATEGORY_LAUNCHER);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.setComponent(cmp);
//                startActivity(intent);
//            } catch (ActivityNotFoundException e) {
//                ToastUtils.showShort("检查到您手机没有安装微信，请安装后使用该功能");
//            }

            WXSDKManager.shareApp(InviteFriendsActivity.this);
        });

        binding.tvCopy.setOnClickListener(v -> {
            ClipboardUtils.copyText(Constants.APK_PAGE_URL);
            ToastUtils.showShort("下载地址已粘贴到剪贴板");
        });

        binding.ivQrcode.setOnClickListener(view -> {
            // TODO 保存二维码到相册
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon_qrcode_sample);
            File appTempDir = new File(APP_TEMP_DIR);
            if (!appTempDir.exists()) {
                appTempDir.mkdir();
            }

            File qrFile = new File(appTempDir, "app_download_qr.jpg");
            try {
                FileOutputStream fos = new FileOutputStream(qrFile);
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), qrFile.getAbsolutePath(), qrFile.getName(), "畅电销");
                ToastUtils.showShort("保存成功，记得分享给好友哟！");
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(qrFile.getAbsolutePath())));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
